<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="55px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="标题" prop="title">
            <el-input v-model="dataForm.title" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="上传" prop="logo">
            <el-upload
              class="avatar-uploader"
              :action="constant.uploadUrl"
              :headers="{ Authorization: cache.getToken() }"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="dataForm.logo" :src="dataForm.logo" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-col>
        <richText ref="richTextRef"></richText>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="essaySubmit()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import constant from '@/utils/constant'
import cache from '@/utils/cache'
import richText from '@/components/rich-text-editor/index.vue'
import type { UploadProps } from 'element-plus'
import { essaySubmitApi, getEssayApi } from '@/api/sys/essay'
const richTextRef = ref()

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = ref<any>({
  id: '',
  detail: '',
  logo: '',
  status: 1,
  title: ''
})

const init = (id?: number) => {
  visible.value = true
  dataForm.value.id = ''
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
    richTextRef.value.valueHtml = ''
  }
  if (id) {
    getDeviceRenewRecorder(id)
  }
}

const handleAvatarSuccess = (res) => {
  dataForm.value.logo = res.data.url
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (file) => {
  if (file.size / 1024 / 1024 / 1024 / 1024 > 1) {
    ElMessage.error('文件大小不能超过100M')
    return false
  }
  return true
}

const getDeviceRenewRecorder = (id: number) => {
  getEssayApi(id).then((res) => {
    dataForm.value = res.data
    richTextRef.value.valueHtml = dataForm.value.detail ? dataForm.value.detail : ''
  })
}
const dataRules = ref({
  title: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const essaySubmit = () => {
  dataForm.value.detail = richTextRef.value.valueHtml
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    essaySubmitApi(dataForm.value).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
<style>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
